import { Controller, Get, Param, Post, Delete, Body, Put } from '@nestjs/common';
import { User } from './user.interface';
import { UserService } from './service/user.service';
import { UserIdPipe } from './pipes/user-id.pipe';
import { CreateUserDto } from './dtos/create-user.dto';
import { async } from 'rxjs/internal/scheduler/async';

@Controller('users')
export class UserController {

  constructor(private readonly usersService: UserService) { }

  @Get()
  async findAll(): Promise<User[]> {
    return await this.usersService.findAll()
  }

  // @Get(':id')
  // async findOne(@Param() params): Promise<User> {
  //   let id = parseInt(params.id)

  //   if(isNaN(id) || typeof id!== 'number' || id<=0) {
  //     throw new ApiException('用户ID无效', ApiErrorCode.USER_ID_INVALID, HttpStatus.BAD_REQUEST)
  //   }

  //   return await this.usersService.findOne(params.id)
  // }

  @Get(':id')
  async findOne(@Param('id', new UserIdPipe()) id): Promise<User> {

    return await this.usersService.findOne(id)
  }

  @Post()
  async create(@Body() user: CreateUserDto): Promise<User> {
    return await this.usersService.create(user)
  }

  @Put()
  async edit(@Body() user: CreateUserDto): Promise<User> {
    return await this.usersService.edit(user)
  }

  @Delete(':id')
  async remove(@Param('id', new UserIdPipe())id: number) {
    console.log(id)
    return await this.usersService.remove(id)
  }
}
